﻿using System;
using System.Web.UI.WebControls;
using BikeBouncer.BusinessLayer;
using BikeBouncer.BusinessLayer.Entities;
using BikeBouncer.BusinessLayer.Enums;
using BikeBouncer.Site.Enums;

namespace BikeBouncer.Site
{
	public partial class Default_aspx : BasePage
	{
		#region Main methods

		protected void Page_Load(Object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				RegisterScript(ScriptSource.ColorBox);

				GetItems();
			}
		}

		#endregion

		#region Get random stolen and lost items

		protected void ListItems_ItemDataBound(Object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			{
				return;
			}

			Item item = (Item)e.Item.DataItem;
			Image img = (Image)e.Item.FindControl("ImgItem");
			Literal title = (Literal)e.Item.FindControl("LitItemTitle");
			Literal status = (Literal)e.Item.FindControl("LitItemStatus");
			Literal date = (Literal)e.Item.FindControl("LitItemDate");
			Literal location = (Literal)e.Item.FindControl("LitItemLocation");
			HyperLink addnote = (HyperLink)e.Item.FindControl("LinkAddNote");
	
			NoteCollection notes = new NoteCollection(DataAccess.Note_Get(item, item.StatusDate, 10));

			if (notes.Count < 1)
			{
				ListMissingItems.Visible = false;
				LitHaveYouSeen.Visible = false;

				return;
			}

			Note n = notes[notes.Count - 1];

			title.Text = item.Summary;

			img.ImageUrl = item.PictureUrl;
			status.Text = GetGlobalResourceObject("Messages", "Status" + item.Status).ToString();
			date.Text = n.ReferenceDate.ToShortDateString();
			location.Text = n.Address.ToString(", ");

			addnote.NavigateUrl = "/BoxAddNote.aspx?refid=" + n.Id;
			addnote.ToolTip += " (" + item.Summary + ")";
		}

		private void GetItems()
		{
			Address a = new Address();

			a.City.Title = SessionManager.CurrentCity;
			a.City.Country = SessionManager.CurrentCountry;

			String key = "MissingItems-" + a.City.Title + "-" + a.City.Country;

			ItemCollection cached = GetCache(key) as ItemCollection;

			if (cached == null)
			{
				ItemCollection ic = new ItemCollection(DataAccess.Item_Get(a, ItemStatus.Stolen));

				if (ic.Count < 5)
				{
					ic.AddRange(new ItemCollection(DataAccess.Item_Get(a, ItemStatus.Lost)));
				}

				cached = GetItemsWithPicture(ic);

				if (cached.Count > 0)
				{
					SetCache(key, cached, CacheDuration.Long);
				}
			}

			if (cached.Count > 0)
			{
				ListMissingItems.DataSource = cached;
				ListMissingItems.DataBind();
			}
			else
			{
				LitHaveYouSeen.Visible = false;
			}
		}

		private ItemCollection GetItemsWithPicture(ItemCollection ic)
		{
			ItemCollection items = new ItemCollection();

			while (items.Count < 3 && ic.Count > 0)
			{
				Item i = ic[new Random().Next(0, ic.Count)];

				if (i.HasPicture)
				{
					items.Add(i);
				}
				
				ic.Remove(i);
			}

			return items;
		}

		#endregion
	}
}